package com.mrzhou.file.service.impl;

import com.amazonaws.HttpMethod;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.emc.object.Method;
import com.emc.object.Range;
import com.emc.object.s3.S3Client;
import com.emc.object.s3.S3ObjectMetadata;
import com.emc.object.s3.bean.CannedAcl;
import com.emc.object.s3.bean.ListObjectsResult;
import com.emc.object.s3.bean.PutObjectResult;
import com.emc.object.s3.request.PresignedUrlRequest;
import com.mrzhou.common.entity.file.FileTree;
import com.mrzhou.core.tool.utils.DateUtil;
import com.mrzhou.file.props.FileAwsProperties;
import com.mrzhou.file.service.IFileService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.LinkedList;
import java.util.List;

@Service
@Slf4j
public class FileServiceImpl implements IFileService {

    @Autowired
    private AmazonS3 amazonS3;
    @Autowired
    private S3Client s3Client;
    @Autowired
    private FileAwsProperties props;

    @Override
    public List<FileTree> fileTreeListHtml(String parentPath) {
        // 创建链表集合, 加快插入的速度
        List<FileTree> tree = new LinkedList<>();
        File parentPathFile = new File(parentPath);
        if(parentPathFile.exists()) {
            // 获得父路径下的子路径
            File[] childFile = parentPathFile.listFiles();
            if(childFile == null)
                return tree;
            for(File child: childFile) {
                if(child.isFile()) {
                    FileTree fileTree = new FileTree();
                    String html = "<span id='" + child.toURI() + "' style='color:#FD7F3E'>" + child.getName() + "</span>";
                    fileTree.setText(html);
                    tree.add(fileTree);
                } else {
                    FileTree fileTree = new FileTree();
                    fileTree.setText(child.getName());
                    List<FileTree> childTree = fileTreeListHtml(child.getPath());
                    fileTree.setNodes(childTree);
                    tree.add(fileTree);
                }
            }
            return tree;
        } else {
            return tree;
        }
    }

    @SneakyThrows
    public String awsUploadFile(MultipartFile file) {
        String key = props.getPathPrefix() + "/" +System.currentTimeMillis() + file.getOriginalFilename();
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentType(file.getContentType());
        objectMetadata.setContentLength(file.getSize());
        amazonS3.putObject(new PutObjectRequest(props.getBucket(), key, file.getInputStream(), objectMetadata).withCannedAcl(CannedAccessControlList.PublicRead));
        GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(props.getBucket(), key).withMethod(HttpMethod.GET);
        log.info("文件的过期时间为: {}", request.getExpiration());
        URL url = amazonS3.generatePresignedUrl(request);
        return url.toString();
    }

    @SneakyThrows
    public String ecsUploadFile(MultipartFile file) {
        String key = props.getPathPrefix() + "/" +System.currentTimeMillis() + file.getOriginalFilename(); // 生成路径key
        S3ObjectMetadata objectMetadata = new S3ObjectMetadata();
        objectMetadata.setContentType(file.getContentType());
        objectMetadata.setContentLength(file.getSize());
        com.emc.object.s3.request.PutObjectRequest request = new com.emc.object.s3.request.PutObjectRequest(props.getBucket(), key,file.getInputStream())
                .withObjectMetadata(objectMetadata)
                .withCannedAcl(CannedAcl.PublicRead);
        s3Client.putObject(request); // 上传对象

        URL url = s3Client.getPresignedUrl(props.getBucket(), key, DateUtil.parse("3000-01-01 00:00:00")); // 获取预览的url
        return url.toString();
    }

    public Object ecsListObjects() {
        ListObjectsResult result = s3Client.listObjects(props.getBucket());
        return result;
    }

}
